package com.resys.server.controller.customer;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.resys.annotation.GlobalInterceptor;
import com.resys.entity.dto.TokenCustomerDto;
import com.resys.entity.po.Comment;
import com.resys.entity.query.CommentQuery;
import com.resys.entity.vo.ResponseVO;
import com.resys.exception.BusinessException;
import com.resys.server.controller.ABaseController;
import com.resys.server.websocket.WebSocketServer;
import com.resys.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@RestController("commentController")
@RequestMapping("/comment")
@Api(tags= "客户模块-评论接口")
public class CommentController extends ABaseController {
    @Resource
    private CommentService commentService;
    @Resource
    private WebSocketServer webSocketServer;
    @ApiOperation("获取首页一级评论")
    @GetMapping("/getFrtComment")
    public ResponseVO getComment(Integer roomId, Integer pageNo) {
        CommentQuery query = new CommentQuery();
        query.setRoomId(roomId);
        query.setType(0);
        query.setPageSize(10);
        query.setPageNo(pageNo);
        query.setOrderBy("create_at desc");
        return getSuccessResponseVO(commentService.findListByPage(query));
    }

    @ApiOperation("获取二级评论")
    @GetMapping("/getSecondComment")
    public ResponseVO getSecondComment(String commentId) {
        CommentQuery query = new CommentQuery();
        query.setCommentpid(commentId);
        query.setType(1);
        query.setOrderBy("create_at desc");
        return getSuccessResponseVO(commentService.findListByParam(query));

    }


    @ApiOperation("获取评论数量")
    @GetMapping("/getCommentCount")
    public ResponseVO getCommentCount(Integer roomId) {
        CommentQuery query = new CommentQuery();
        query.setRoomId(roomId);
        return getSuccessResponseVO(commentService.findCountByParam(query));
    }
    @ApiOperation("获取二级评论数量")
    @GetMapping("/getSecondCommentCount")
    public ResponseVO getSecondCommentCount(String commentId) {
        CommentQuery query = new CommentQuery();
        query.setCommentpid(commentId);
        return getSuccessResponseVO(commentService.findCountByParam(query));
    }
    
    @ApiOperation("发布二级评论")
    @PostMapping("/commentsubmit")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO commentsubmit(String content,String commentId,String replyId) {
        TokenCustomerDto customer = getTokenCustomer();
        commentService.commentsubmit(content, customer.getCustomerId(), commentId, replyId,1);
        return getSuccessResponseVO(null);
    }
    @ApiOperation("发布一级评论")
    @PostMapping("/commentadd")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO commentadd(String content,Integer orderId,Integer likesCount) {
        TokenCustomerDto customer = getTokenCustomer();
        commentService.commentadd(content, customer.getCustomerId(), likesCount,orderId,1);
        return getSuccessResponseVO(null);
    }
    @ApiOperation("检验该评论是否我发的")
    @PostMapping("/IsMine")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO IsMine(String commentId) {
        TokenCustomerDto customer = getTokenCustomer();
        Comment comment = commentService.getCommentByCommentId(commentId);
        if (customer == null || comment == null) {
            throw new BusinessException("用户或者评论不存在");
        }
        if(customer.getCustomerId().equals(comment.getCommitId())&&comment!=null&&customer!=null){
            return getSuccessResponseVO("yes");
        }else{
            return getSuccessResponseVO("no");
        }

    }


    @ApiOperation("删除评论")
    @PostMapping("/del")
    @GlobalInterceptor(checkLogin = true)
    public void del(String commentId) {
        TokenCustomerDto customer = getTokenCustomer();
        Comment comment = commentService.getCommentByCommentId(commentId);
        //只有评论者才能删除评论
        if(customer.getCustomerId().equals(comment.getCommitId())&&comment!=null){
            commentService.del(commentId);
        }

    }


}
